﻿
using HBase;
using HBase.db;
using hCommand;
using Net.http;
using Persistence;
using System.Collections.Generic;

namespace WebApi
{
    [FormAuthenticationFilter]

    [RoutePrefix("item")]
    public class itemController :BaseApiController {
        IItemServer iitemServer = WMSDalFactray.getDal<IItemServer>();
        //物料分页查询
        [HttpPost, Route("FindPage")]
        public BaseResult FindPage([FromBody] CoreItemDto dto) {
            var info = iitemServer.QueryMaterialPage<CoreItem>(dto);
            return BaseResult.Ok(info);
        }

        [HttpPost, Route("findAll")]
        public BaseResult findAll() {
            var info = iitemServer.getAllItem();
            return BaseResult.Ok(info);
        }

        //物料保存
        [HttpPost, Route("initArea"), ControlName("生成库区")]
        public BaseResult initArea([FromBody] CoreItem coreItem) {

            coreItem = MyCache.getValue<CoreItem>(coreItem.id);
            if( coreItem.classifyId != 0 ) {
                CoreClassifyAreaDto dto = new CoreClassifyAreaDto();
                dto.classifyId = coreItem.classifyId;
                dto.pageNum = 1;
                dto.pageSize = 10;
                Page<CoreClassifyArea> page = iitemServer.QueryClassifyAreaPage(dto);
                foreach( CoreClassifyArea a in page.content ) {

                    CoreWhArea areaold = iitemServer.getById<CoreWhArea>(a.areaId);
                    areaold.AreaName = coreItem.name;
                    iitemServer.updateNotNull(areaold);
                    break;
                }

                return BaseResult.Ok("物料已经关联");
            }

            CoreClassify classify = new CoreClassify(); 
            classify.name = coreItem.name;
            iitemServer.insert(classify);
            coreItem.classifyId = classify.id;
            iitemServer.updateNotNull(coreItem);

            CoreWhArea area = new CoreWhArea(); 
            area.AreaName = coreItem.name;
            iitemServer.insert(area);


            CoreClassifyArea classArea = new CoreClassifyArea();
            classArea.areaId = area.id;
            classArea.classifyId = classify.id;
           
            iitemServer.insert(classArea);
            MyCache.setDirty<CoreItem>(classArea);
            return BaseResult.Ok("生成成功");


        }

        //物料保存
        [HttpPost, Route("Save"), ControlName("物料保存")]
        public BaseResult Save([FromBody] CoreItem coreItem) {
            if( coreItem.id == 0 ) {
                long id = iitemServer.AddMaterial(coreItem);
                MyCache.setDirty<CoreItem>(id);
                return BaseResult.Ok(id);
             
            } else {
                iitemServer.UpdateInfo(coreItem);
                MyCache.setDirty<CoreItem>(coreItem);
                return BaseResult.Ok(coreItem.id);
            }

        }

        //物料导入
        [HttpPost, Route("ImportList"), ControlName("物料导入")]
        public BaseResult ImportList([FromBody] List<CoreItem> coreItemList) {
            string message = string.Empty;
            bool result = iitemServer.ImportList(coreItemList ,out message);
            MyCache.setDirty<CoreItem>(result);
            if ( result ) {
                return BaseResult.Ok("操作成功!");
            } else {
                return BaseResult.Error(message);
            }

        }
        //物料恢复
        [HttpPost, Route("Restore"), ControlName("物料恢复")]
        public BaseResult Restore([FromBody] CoreItem coreItem) {
            iitemServer.UpdateActive(coreItem);
            MyCache.setDirty<CoreItem>(coreItem);
            return BaseResult.Ok("ok");
        }
        //物料禁用
        [HttpPost, Route("Disable"), ControlName("物料禁用")]
        public BaseResult Disable([FromBody] CoreItem coreItem) {
            iitemServer.UpdateActive(coreItem);
            MyCache.setDirty<CoreItem>(coreItem);
            return BaseResult.Ok("ok");
        }
        //物料删除
        [HttpPost, Route("Delete"), ControlName("物料删除")]
        public BaseResult Delete([FromBody] List<CoreItem> coreItemList) {
            foreach( var core in coreItemList ) {
                CoreItem coreItem = iitemServer.FindCoreItemById(core.id);

            }
           iitemServer.DeleteCoreItem(coreItemList);
            MyCache.setDirty<CoreItem>(coreItemList);
            return BaseResult.Ok("ok");
        }

        //导出
        [HttpPost, Route("GetExportList")]
        public BaseResult GetExportList([FromBody] CoreItemDto dto) {
            var info = iitemServer.GetExportList(dto);
            return BaseResult.Ok(info);
        }

    }
}